package com.hospital.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import com.hospital.system.entity.User;
import com.hospital.system.service.UserService;

public class UserRealm extends AuthorizingRealm {
	@Autowired
	private UserService userService;
	//认证
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//登录用户提交的用户名
		String loginname = (String) token.getPrincipal();
		//根据用户名查找对应用户
		User user = userService.findUserByLoginname(loginname);
		System.out.println(user);
		if(user==null) {
			return null;
		}
		//数据库查询到的密码
		String credentials = user.getPassword();
		//数据库查询密码加的盐
		String saltInDatabase = user.getSalt();
		ByteSource salt = ByteSource.Util.bytes(saltInDatabase) ;
		SimpleAuthenticationInfo authenticationInfo = 
				new SimpleAuthenticationInfo(loginname, credentials, salt, this.getName());
		return authenticationInfo;
	}
	
	//授权
		@Override
		protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
			return null;
		}
}
